package com.bangcommunity.bbframe.mlog.logger;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationHome;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author tanghc
 * @date 18/1/29
 */
public abstract class LogFileAccessor {

    private static final String LOG_PATH = "/opt/logs/";
    private static final String LOG_PATH_PARAM = "mlogPath";

    static final Logger logger = LoggerFactory.getLogger(LogFileAccessor.class);

    /**
     * 返回文件路径。 1、判断路径是否有文件,有 则返回,无则判断是否能创建文件,能则返回。此处路径优先"/opt/logs/",然后系统参数mlogPath
     * 2、如果上面两个路径都不行则使用相对路径./filename
     */
    public static String getFilePath(String fileName) {
        String filePathName = LOG_PATH + fileName;
        if (existOrCanCreate(filePathName)) {
            logger.info("filePathName1=" + filePathName);

            return filePathName;
        }
        String property = System.getProperty(LOG_PATH_PARAM);
        logger.debug("property=" + property);
        if (StringUtils.isNotBlank(property)) {
            if (!property.endsWith(File.pathSeparator)) {
                property = property + File.pathSeparator;
            }
            filePathName = property + fileName;
            if (existOrCanCreate(filePathName)) {
                logger.info("filePathName2=" + filePathName);

                return filePathName;
            }
        }

        //获取跟目录
        String path = null;
//            path = ResourceUtils.getURL("classpath:").getPath().toString();
        ApplicationHome home = new ApplicationHome(LogFileAccessor.class);
        path = home.getSource().getAbsolutePath();
        if (StringUtils.isNotEmpty(path)) {
            filePathName = path + "mlog" + File.pathSeparator + fileName;
            if (existOrCanCreate(filePathName)) {
                logger.info("filePathName3=" + filePathName);
                return filePathName;
            }
        }

//        filePathName = "mlog" + File.pathSeparator + fileName;
//        filePathName = "." + File.pathSeparator + "mlog" + File.pathSeparator + fileName;

        logger.info("filePathName4=" + filePathName);
        return filePathName;

    }

    private static boolean existOrCanCreate(String filePathName) {
        File file = new File(filePathName);
        if (file.exists()) {
            return true;
        }
        try {
            if (file.createNewFile()) {
                return true;
            }
        } catch (IOException e) {
            logger.warn("create log file fail: {}",filePathName);
        }
        return false;
    }

    public static void main(String[] args) {
        String path = "";
        String path1 = "";
        try {
            path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
            path1 = ResourceUtils.getURL("classpath:").getPath().toString();

            ApplicationHome home = new ApplicationHome(LogFileAccessor.class);
            File jarFile = home.getSource();
            System.out.println("jarFile" + jarFile.getAbsolutePath());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}